home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Commands / Date.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  15.2 KB  |  557 lines

  1.  
  2. //
  3. // Copyright 1999 Macromedia, Inc. All rights reserved. 
  4. //
  5. // ----------------------------------------------------
  6. //
  7. // Date.js
  8. //
  9. //This object inserts the current date and optionally time
  10. //and day at the current insertion point. An Update Automatically
  11. //On Save Option will update the date when the document is saved.
  12. //
  13. //
  14. //Localization Note:
  15. //Read the comments in the initGlobals function for instructions
  16. //on controlling the order of the date formats in the Date Format list,
  17. //as well as specifying that certain date formats should not appear
  18. //in a localized language.
  19.  
  20.  
  21.  
  22. //*********************GLOBAL VARIABLES***********************
  23.  
  24. //see initGlobals for info on global vars
  25.  
  26. var helpDoc = MM.HELP_objDate;
  27.  
  28. //global arrays
  29. var GarrDateFormats; 
  30. var GarrDayFormats;
  31. var GarrTimeFormats;
  32.  
  33. //form element objects
  34. var GlistDateFormats;
  35. var GselDayFormats;
  36. var GselTimeFormats;
  37. var GcbUpdate;
  38.  
  39. //global strings needed for accessor functions
  40. var GstrFullDate;
  41. var GstrDateID;
  42.  
  43. function initGlobals(){
  44.  
  45. //This extension requires special localization work to adjust the order and/or conent
  46. //of the day,date, and time drop-down menus.
  47. //---------------------------------------------------------------------------------
  48. //
  49. //BEGIN LOCALIZATION WORK
  50. //
  51. //The GarrDateFormats array controls the order the date formats are shown in the UI
  52. //and the initially selected format.
  53. ///
  54. //1.Re-arrange the localized language to be the first item in the list. 
  55. //(The first item is selected by default when the dialog loads.)
  56. //
  57. //2.Place any other formats that will be commonly used by that locale
  58. //as the second, third, fourth items, etc.
  59. //If the locale uses day-month-year instead of month-day-year,
  60. //move the month-day-year formats lower on the list
  61. //
  62. //3.Delete any formats that should not be shown. For instance,
  63. //the British1 format uses an abbreviated month. If this locale never
  64. //abbreviates its months (and therefore Arr_Abbr_Months cannot be translated),
  65. //delete the Engish1 format. 
  66.  
  67.     GarrDateFormats = new Array("American1",    
  68.                                 "English2",
  69.                                 "English1",
  70.                                 "American3",
  71.                                 "American2",
  72.                                 "ISO8601",
  73.                                 "Spanish1",
  74.                                 "French1",
  75.                                 "German1",
  76.                                 "Brazilian1",
  77.                                 "Italian1",
  78.                                 "Swedish1",
  79.                                 "Japanese1"
  80.                                 );
  81.  
  82.    //Date Formating Examples:
  83.    //American1       March 7, 1974
  84.    //English1        7-mar-74
  85.    //English2        07-Mar-1974
  86.    //American2       3/7/74
  87.    //American3       03/07/1974
  88.    //ISO8601         1974-03-07
  89.    //Spanish1        7/3/74 //also a common English format
  90.    //French1         7/03/74
  91.    //Brazilian1      07.03.74
  92.    //German1         07.03.1974
  93.    //Italian1        7-03-1974
  94.    //Swedish1        7 march, 1974        
  95.    //Japanese1       74-03-07
  96.    
  97.    //--------------------------------
  98.    
  99.    ///The GarrDateFormats array controls the order the day formats are shown in the UI
  100.    //and the initially selected format. Repeat the same instructions as above - that is,
  101.    //re-arrange or delete the items according to the localized language
  102.    
  103.    //For instance, if a language does not have the concept of abbreviated days, then
  104.    //delete or comment out all of the abbreviated formats. If a language does not ever 
  105.    //use the "," as a separator, then delete or comment out all of the comma formats.
  106.    
  107.     GarrDayFormats = new Array("NoDay",
  108.                                "FullDayComma",
  109.                                "FullDay",
  110.                                "AbbrDayComma",
  111.                                "AbbrDay",
  112.                                "LowAbbrDayComma",
  113.                                "LowAbbrDay"
  114.                                 );
  115.                                   
  116.    //Day Formatting Examples:
  117.    //NoDay           [No Day]
  118.    //FullDayComma     Monday,
  119.    //FullDay          Monday
  120.    //AbbrDayComma1    Mon,
  121.    //AbbrDay1         Mon
  122.    //AbbrDayComma2    mon,
  123.    //AbbrDay2         mon
  124.    
  125.    //-------------------------------
  126.    
  127.    //The GarrTimeFormats array controls the order of the time formats.
  128.    //For countries that use military time, consider switching the
  129.    //second and third items.
  130.    
  131.    
  132.    GarrTimeFormats = new Array("NoTime",
  133.                                "AMPMTime",
  134.                                "MilitaryTime"         
  135.                                );
  136.  
  137.    //END LOCALIZATION WORK
  138.    
  139.    //-----------------------------------------------------------------------------
  140.    
  141.    //initialize global form elements
  142.    theForm = document.forms[0]; 
  143.    GlistDateFormats = theForm.DateFormats;
  144.    GselDayFormats   = theForm.DayFormats;
  145.    GselTimeFormats  = theForm.TimeFormats;
  146.    GcbUpdate        = theForm.Update;
  147. }
  148.  
  149.  
  150.  
  151. //*********************** API ***********************
  152.  
  153. //function: commandButtons
  154. //description: generic API function, returns string to be inserted at IP
  155.  
  156. function commandButtons(){
  157.    return new Array(BTN_OK,         "setDateStr();window.close()",
  158.                     BTN_Cancel,     "window.close()",
  159.                     BTN_Help,       "displayHelp()"    );
  160.  
  161.  
  162. }
  163.  
  164. //***********************ACCESSOR FUNCTIONS***********************
  165.  
  166. function getDateStr(){
  167.    return GstrFullDate;
  168. }
  169.  
  170. function getDateID(){
  171.    return GstrDateID;
  172. }
  173.  
  174. //***********************LOCAL FUNCTIONS***********************
  175.  
  176. //function: setDateStr
  177. //description: called from OK button of dialog,
  178. //sets the global GstrFullDate variable
  179.  
  180. function setDateStr(){
  181.      var dateStr = "";
  182.   var dateID = "";
  183.     var now = new Date();
  184.     
  185.     var dayFormat  = getSelectedOptionAttr(GselDayFormats,"value");
  186.     var dateFormat = getSelectedOptionAttr(GlistDateFormats,"value");
  187.     var timeFormat = getSelectedOptionAttr(GselTimeFormats,"value");        
  188.                      
  189.     //create the date that is inserted
  190.     dateStr += createDayStr(now,dayFormat);
  191.     dateStr += createDateStr(now,dateFormat);
  192.     dateStr += createTimeStr(now,timeFormat);
  193.     
  194.     //dateID is inserted into the format attribute of the lock
  195.     //and also placed in the opening comment
  196.     dateID = createDateID(dayFormat,dateFormat,timeFormat);
  197.     
  198.     //if Update Automatically On Save is visible and checked,
  199.     //add locks around the date
  200.     if (GcbUpdate!=null && GcbUpdate.checked){
  201.        dateStr = addLockMarkup(dateStr,dateID);   
  202.     }
  203.     
  204.     //assign to global variables accessed by accessor functions
  205.     GstrFullDate = dateStr;
  206.     GstrDateID = dateID;  
  207. }
  208.  
  209.  
  210.  
  211. //function: addLockMarkup
  212. //description: adds correct lock markup to the date that is 
  213. //inserted
  214.  
  215. function  addLockMarkup(dateStr,dateID){
  216.  
  217.    var openBracket = "%3C";
  218.    var closeBracket = "%3E";
  219.    var quote = "%22";
  220.  
  221.    var openComment = openBracket + '!-- #BeginDate ' +
  222.                      'format:' + dateID + ' --' + closeBracket;
  223.    var closeComment = openBracket + '!-- #EndDate --' + closeBracket;
  224.    var origAttr = openComment + dateStr + closeComment;
  225.    
  226.    var openLock = '<MM:BeginLock type="mmdate" format="' + dateID + 
  227.                   '" orig="' + origAttr + '">';
  228.    var closeLock =  '<MM:EndLock>';
  229.    
  230.    return openLock +  dateStr + closeLock;
  231. }
  232.  
  233.  
  234.  
  235. //function: initializeUI
  236. //description: initializes the global variables, and populates
  237. //the UI with date format examples.
  238.  
  239. function initializeUI(){
  240.    //return if already initialized
  241.    //(this happens if command is called from PI)
  242.    if (GarrDateFormats){
  243.       GlistDateFormats.focus();
  244.       return;
  245.    }
  246.       
  247.    initGlobals(); //initialize global variables
  248.    populateUI();  //populate UI
  249. }
  250.  
  251.  
  252.  
  253. //function: populateUI
  254. //description: populate the UI with date format examples
  255.  
  256. function populateUI(){
  257.    var dateFormatsArr = GarrDateFormats;  //shorter names easier to work with
  258.    var dayFormatsArr  = GarrDayFormats;
  259.    var timeFormatsArr = GarrTimeFormats;
  260.    
  261.    var nDateFormats = dateFormatsArr.length;
  262.    var nDayFormats  = dayFormatsArr.length;
  263.    var nTimeFormats = timeFormatsArr.length;
  264.    
  265.    var dateObj = new Date("74","2","7","22","18");  //examples are for March 7, 1974
  266.    var dateStr = "",timeStr="",dayStr=""; 
  267.    
  268.     //populate day format list in UI
  269.    //the first line creates an array of formatted dayes
  270.    //(the createDayStr function is overloaded to return one item
  271.    //or an array, therefore, unfortunately the function name isn't 
  272.    //always entirely accurate)
  273.    dayFormatsArr = createDayStr(dateObj,dayFormatsArr,true);
  274.    var counter = 0;
  275.    for (i in dayFormatsArr){
  276.       if (!dayFormatsArr[i].prototype){ 
  277.          // Val 13-aug-99 use temporary for speedier UI loading
  278.          var curr = new Option(dayFormatsArr[i]);
  279.          curr.value = i;
  280.          GselDayFormats.options[ counter++ ] = curr;
  281.       }
  282.    }
  283.    
  284.    //populate date format list in UI
  285.    //the first line creates an array of formatted dates
  286.    //(the createDateStr function is overloaded to return one item
  287.    //or an array, therefore, unfortunately, the function name isn't 
  288.    ///always entirely accurate)
  289.    dateFormatsArr  = createDateStr(dateObj,dateFormatsArr);
  290.    counter = 0;
  291.    for (i in dateFormatsArr){
  292.       if (!dateFormatsArr[i].prototype){
  293.          // Val 13-aug-99: use a temporary to make initialization faster,
  294.          // and only assign to the formats array once.
  295.            var currObj = new Option(dateFormatsArr[i]);
  296.          currObj.value = i;
  297.          GlistDateFormats.options[ counter++ ] = currObj;
  298.       }
  299.    }
  300.    
  301.    //populate time format list
  302.    for (i=0;i<nTimeFormats;i++){
  303.       timeStr = createTimeStr(dateObj, timeFormatsArr[i], true);
  304.       GselTimeFormats.options[ i ] = new Option(timeStr);
  305.       GselTimeFormats.options[ i ].value = timeFormatsArr[i];
  306.    }
  307.   
  308.    
  309.    //select first option of each menu
  310.    GselDayFormats.selectedIndex = 0;
  311.    GlistDateFormats.selectedIndex = 0;
  312.    GselTimeFormats.selectedIndex = 0;
  313.    
  314.    //put focus in date formats field
  315.    GlistDateFormats.focus();
  316. }
  317.  
  318.  
  319.  
  320. //function: lead
  321. //description: given a one or two digit number,
  322. //adds a leading 0 if a 1 digit number
  323.  
  324. function lead(num){
  325.    if (num.toString().length == "1")
  326.       return "0" + num;
  327.    return ( num );
  328. }
  329.  
  330.  
  331. //function: createDateStr
  332. //description: given a date obj and a date format or formats
  333. //returns an array with the correctly formatted date strings
  334. //overloaded: dateFormat can be one item or an array
  335. //if it is one item, returns one item
  336. //if it is an array, returns an array
  337.  
  338. function createDateStr(dateObj,dateFormat){
  339.    var date = dateObj.getDate();
  340.    var day = dateObj.getDay();
  341.    var month = dateObj.getMonth();
  342.    var abbrMonth = ARR_AbbrMonths[ month ];
  343.    var fullMonth = ARR_FullMonths[ month++ ];
  344.    var year = dateObj.getYear();
  345.    var abbrYear = (year<100)? year : year.toString().substring(1);
  346.    var fullYear = (year<100)? "19" + year : year + 1900;
  347.    
  348.    var retVal; //return value;
  349.    
  350.    //the dateFormat argument is overloaded so that it
  351.    //can be either a string or an array. Handle accordingly.
  352.    if (typeof dateFormat == "string"){
  353.       retVal = createCorrectDateFormat(dateFormat,date,day,month,abbrMonth,fullMonth,
  354.                                        year,abbrYear,fullYear);
  355.    } else { //dateFormat is an array
  356.         retVal = new Array();
  357.         dateFormats = dateFormat; //rename
  358.         var nFormats = dateFormats.length;
  359.  
  360.         
  361.         for (var i=0;i<nFormats;i++){
  362.            retVal[dateFormats[i]] = createCorrectDateFormat(dateFormat[i],date,day,month,abbrMonth,
  363.                                                fullMonth,year,abbrYear,fullYear);
  364.             
  365.         }
  366.    }    
  367.    
  368.    return retVal;
  369. }
  370.  
  371.  
  372. //function: createCorrectDateFormat
  373. //description: returns a correclty formatted date string
  374.  
  375. function createCorrectDateFormat(dateFormat,date,day,month,abbrMonth,fullMonth,
  376.                                  year,abbrYear,fullYear,time){
  377.  
  378.     var dateStr = "";
  379.    
  380.     switch (dateFormat){
  381.     
  382.        case "American1":  // Thursday, March 7, 1974
  383.           dateStr += fullMonth + " " + date + ", " + fullYear;
  384.           break;
  385.           
  386.        case "American2":  // 3/7/74
  387.           dateStr += month + "/" + date + "/" + abbrYear;
  388.           break;
  389.           
  390.        case "American3":  // 03/07/1974
  391.           dateStr += lead(month) + "/" + lead(date) + "/" + fullYear;
  392.           break;
  393.  
  394.        case "ISO8601":  // 1974-03-07
  395.           dateStr += fullYear + "-" + lead(month) + "-" + lead(date);
  396.           break;
  397.           
  398.        case "English1":  // 7-mar-74
  399.           dateStr += date + "-" + abbrMonth + "-" + abbrYear;
  400.           break;
  401.       
  402.        case "English2":  // 07-Mar-1974
  403.           abbrMonth = abbrMonth.charAt(0).toUpperCase()+abbrMonth.substring(1);
  404.           dateStr += lead(date) + "-" + abbrMonth + "-" + fullYear;
  405.           break;
  406.           
  407.        case "Spanish1":  // 7/3/74 
  408.           dateStr += date + "/" + month + "/" + abbrYear;
  409.           break;
  410.           
  411.        case "French1":  //  7/03/74
  412.           dateStr += date + "/" + lead(month) + "/" + abbrYear;
  413.           break;
  414.           
  415.        case "Italian1":  // 7-03-1974
  416.           dateStr += date + "-" + lead(month) + "-" + fullYear;
  417.           break;
  418.           
  419.        case "Brazilian1":  // 07.03.74
  420.           dateStr += lead(date) + "." + lead(month) + "." + abbrYear;
  421.           break;
  422.           
  423.        case "German1":  // 07.03.1974
  424.           dateStr += lead(date) + "." + lead(month) + "." + fullYear;
  425.           break;
  426.           
  427.        case "Japanese1":  // 74-03-07
  428.           dateStr += abbrYear + "-" + lead(month) + "-" + lead(date);
  429.           break;
  430.                     
  431.        case "Swedish1":  //  7 March, 1974
  432.           dateStr +=  date + " " + fullMonth + ", " + fullYear;
  433.           break;
  434.           
  435.        default:
  436.           break;
  437.    }
  438.    
  439.    return dateStr;
  440.  
  441. }
  442.  
  443.  
  444.  
  445. //function: createDayStr
  446. //description: see createDateStr notes. Except of course this function
  447. //returns a correctly formatted day (or days) instead of a date
  448.  
  449. function createDayStr(dateObj,dayFormat,bPreview){
  450.    day = dateObj.getDay();
  451.    fullDay = ARR_FullDays[day];
  452.    abbrDay = ARR_AbbrDays[day];
  453.  
  454.    if (typeof dayFormat == "string"){
  455.       retVal = createCorrectDayFormat(dayFormat,fullDay,abbrDay,bPreview);
  456.    } else { //dayFormat is an array
  457.         retVal = new Array();
  458.         dayFormats = dayFormat; //rename for clarity
  459.         var nFormats = dayFormats.length;
  460.  
  461.         for (var i=0;i<nFormats;i++){
  462.            retVal[dayFormats[i]] = createCorrectDayFormat(dayFormat[i],fullDay,abbrDay,bPreview);
  463.         }
  464.    }    
  465.    return retVal;
  466. }
  467.  
  468.  
  469.  
  470. //function: createCorrectDayFormat
  471. //description: returns the correctly formatted day format
  472.  
  473. function createCorrectDayFormat(dayFormat,fullDay,abbrDay,bPreview){
  474.  
  475.    var dayStr = "";
  476.    
  477.    switch (dayFormat){
  478.    
  479.       case "NoDay":
  480.          if (bPreview)
  481.             dayStr = "[" + OPTION_NoDay + "]";
  482.          break;
  483.           
  484.       case "FullDayComma":
  485.          dayStr = fullDay + ", ";
  486.          break;
  487.          
  488.       case "FullDay":
  489.          dayStr = fullDay + " ";
  490.          break;
  491.          
  492.       case "AbbrDayComma":
  493.          dayStr = abbrDay + ", ";
  494.          break;
  495.          
  496.       case "AbbrDay":
  497.          dayStr = abbrDay + " ";
  498.          break;
  499.          
  500.       case "LowAbbrDayComma":
  501.          dayStr = abbrDay.toLowerCase() + ", ";
  502.          break;
  503.          
  504.       case "LowAbbrDay":
  505.          dayStr = abbrDay.toLowerCase() + " ";
  506.          break;
  507.          
  508.       default:
  509.          break;
  510.    
  511.    }
  512.  
  513.    return dayStr;
  514.  
  515. }
  516.  
  517.  
  518.  
  519. //function: createTimeStr
  520. //description: given a dateObj and a time format,
  521. //returns the correctly formatted time string
  522. //The time format argument is "a" for AM/PM,
  523. //"m" for military time, and "" for no time
  524.  
  525. function createTimeStr(dateObj,timeFormat,bPreview){
  526.    var hours = dateObj.getHours();
  527.    var minutes = lead(dateObj.getMinutes());
  528.    var timeStr = "";  //return value
  529.    
  530.    switch (timeFormat){
  531.       case "NoTime":
  532.          if (bPreview)
  533.             timeStr = "[" + OPTION_NoTime + "]";
  534.          break;
  535.          
  536.       case "AMPMTime":
  537.          timeStr += (hours>12) ? hours-12 + ":" + minutes + " " + PM : 
  538.                                  hours + ":" + minutes + " " + AM;
  539.          timeStr = " " + timeStr;
  540.          break;
  541.           
  542.       case "MilitaryTime":
  543.          timeStr += " " + hours + ":" + minutes;
  544.          timeStr = " " + timeStr;
  545.          break;
  546.          
  547.       default:
  548.          break;
  549.    }
  550.          
  551.    return timeStr;
  552. }
  553.  
  554.  
  555.  
  556.  
  557.